﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using VideoSystem.Ctrl;


namespace VideoSystem
{
    public partial class Form1 : Form
    {
        public int seconds; // Seconds.  
        public int minutes; // Minutes.  
        public int hours; // Hours not used
        PushCtrl pushCtrl = new PushCtrl();
  

        public Form1()
        {
            InitializeComponent();
        }
        private int CreateOption()
        {
            OptionCtrl optionCtrl = new OptionCtrl();

            // Get value from numericUpDown and convert from decimal to int
            int episodeId = Convert.ToInt32(episodeIdNumericUpDown.Value);
            // Get text from textbox option1
            string option1Text = textBoxOption1.Text;
            // Get text from textbox option2
            string option2Text = textBoxOption2.Text;

            // Call createoption method from optionCtrl with values
            return optionCtrl.CreateOption(episodeId, option1Text, option2Text);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int result = CreateOption();
            if (result == 1)
            {
                savedSuccesfull();
            }
            else
            {
                saveUnsuccesFull();
            }

        }

        private void savedSuccesfull()
        {
            // Initializes the variables to pass to the MessageBox.Show method. 

            string message = "Valgmuligheden gemt i databasen!";
            string caption = "Succes!";
            MessageBoxButtons buttons = MessageBoxButtons.OK;
            DialogResult result;
            // Displays the MessageBox.
            result = MessageBox.Show(message, caption, buttons);

            if (result == System.Windows.Forms.DialogResult.OK)
            {
                episodeIdNumericUpDown.Value = 1;
                textBoxOption1.Clear();
                textBoxOption2.Clear();
            }
        }

        private void saveUnsuccesFull()
        {
            // Initializes the variables to pass to the MessageBox.Show method. 

            string message = "Der er sket en fejl kontakt udvikleren!";
            string caption = "Error!";
            MessageBoxButtons buttons = MessageBoxButtons.OK;
            DialogResult result;
            // Displays the MessageBox.
            result = MessageBox.Show(message, caption, buttons);

            if (result == System.Windows.Forms.DialogResult.OK)
            {

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            showVote();
        }

        private void showVote()
        {
            // Initializes the variables to pass to the MessageBox.Show method. 
            IOptionDb ioptionDb = new OptionDb();
            int episodeId = Convert.ToInt32(episodeIdNumericUpDown.Value);
            string message = "" + ioptionDb.getVoteResult(episodeId) + "";
            string caption = "Resultat af afstemning for episode " + episodeIdNumericUpDown.Value + "       ";
            MessageBoxButtons buttons = MessageBoxButtons.OK;
            DialogResult result;
            // Displays the MessageBox.
            result = MessageBox.Show(message, caption, buttons);
        }

        // Call this method to start the timer
        private void StartTimer()
        {
            // The timer will not start if nothing is presented in the textbox for min and sec
            if ((textBoxMin.Text != "") && (textBoxSec.Text != ""))
            {
                buttonStartVoting.Enabled = true;
                textBoxMin.Enabled = false;
                textBoxSec.Enabled = false;

                try
                {
                    minutes = System.Convert.ToInt32(textBoxMin.Text);
                    seconds = System.Convert.ToInt32(textBoxSec.Text);
                    timer1.Enabled = true;
                    buttonStartVoting.Enabled = false;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
            {
                MessageBox.Show("Ups du har ikke tastet tiden korrekt!");
            }
        }




        private void buttonStartVoting_Click(object sender, EventArgs e)
        {
            
            // Get value from numericUpDown and convert from decimal to int
            int episodeId = Convert.ToInt32(numericUpDownEpisodeIdPush.Value);
            pushCtrl.StartPush(episodeId);
            StartTimer();
        }

        private void timer1_Tick_1(object sender, EventArgs e)
        {
            // Verify if the time didn't pass.
            if ((minutes == 0) && (seconds == 0))
            {
                // If the time is over, clear all settings and fields.
                // Also, show the message, notifying that the time is over.
                timer1.Enabled = false;
                MessageBox.Show("Afstemningen er slut");
                buttonStartVoting.Enabled = true;
                textBoxMin.Text = "0";
                textBoxSec.Text = "0";
                textBoxMin.Enabled = true;
                textBoxSec.Enabled = true;
                labelMin.Text = "00";
                labelSec.Text = "00";
            }
            else
            {
                // Else continue counting.
                if (seconds < 1)
                {
                    seconds = 59;
                    if (minutes == 0)
                    {
                        minutes = 59;
                        if (hours != 0)
                            hours -= 1;

                    }
                    else
                    {
                        minutes -= 1;
                    }
                }
                else
                    seconds -= 1;

                // Display the current values of minutes and seconds in
                // the corresponding fields.
                labelMin.Text = minutes.ToString();
                labelSec.Text = seconds.ToString();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string caption = "Hjælp";
            string helpText = "1. For at gemme valgmuligheder.\n Under indstillinger: Skriv valgmuligeder i de to tekstbokse og vælg hvilken episodeid de tilhører, tryk herefter Gem valgmulighed. \n\n 2. For at starte en afstemning.\n Under Showstart: Sæt afstemningstid og vælg episodeid, tryk herefter Start Afstemning \n\n 3. For at se resultatet af en afstemning.\n Under Indstillinger: vælg episode id og tryk Vis afstemningsresultat";
            MessageBox.Show(helpText, caption);
        }
        

        
 
    

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {

        }

        private void label8_Click(object sender, EventArgs e)
        {

        }

        private void labelSec_Click(object sender, EventArgs e)
        {

        }

        private void label7_Click(object sender, EventArgs e)
        {

        }

        private void labelMin_Click(object sender, EventArgs e)
        {

        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void textBoxSec_TextChanged(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void textBoxMin_TextChanged(object sender, EventArgs e)
        {

        }

        
    }
}